package net.minecraft.src;

import java.util.ArrayList;
import java.util.Random;

public class WorldGenHouse extends WorldGenerator {

	String Floor1[] = { "SXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX" };

	String Floor2[] = { "XXXXX", "X  XX", "D   X", "X   X", "XXXXX" };

	String Floor3[] = { "XXXXX", "X   X", "D FXX", "X   X", "XXXXX" };

	String Floor4[] = { "XXXXX", "X   X", "X   X", "X  XX", "XXXXX" };

	String Floor5[] = { "XXXXX", "XXX X", "XXX X", "XXX X", "XXXXX" };
	
	String Floor6[] = { "XXXXX", "X   X", "X   X", "X   X", "XXXXX" };
	
	String Floor7[] = { "XXXXX", "X   X", "X   X", "X   X", "XUUUX" };
	
	String Floor8[] = { "XXXXX", "X   X", "X   X", "X   X", "XXXXX" };
	
	String Floor9[] = { "XXXXX", "X   U", "X   U", "X   U", "XXXXX" };
	
	String Floor10[] = { "XXXXX", "XXXXX", "XXXXX", "XXXXX", "XXXXX" };

	Object House[] = new Object[10];

	public WorldGenHouse() {
		// add floors to house array
		House[0] = Floor1;
		House[1] = Floor2;
		House[2] = Floor3;
		House[3] = Floor4;
		House[4] = Floor5;
		House[5] = Floor6;
		House[6] = Floor7;
		House[7] = Floor8;
		House[8] = Floor9;
		House[9] = Floor10;
	}

	public void createHouse(World world, int i, int j, int k) {
		for (int cx = 0; cx < 20; cx++) {
			for (int cy = 0; cy < 10; cy++) {
				for (int cz = 0; cz < 20; cz++) {
				world.setBlockWithNotify(i + cx, j + cy, k + cz,0);
				world.setBlockWithNotify(i - cx, j + cy, k - cz,0);
				}
			}		
		}
	
		System.out.println("House Actually Created");
		System.out.println("At X:" + i + " Y:" + j + " Z:" + k);
		// find 'S' aka starting point
		for (int a = 0; a < House.length; a++) {
			for (int b = 0; b < ((String[]) House[a]).length; b++) {
				for (int c = 0; c < ((String[]) House[a])[b].length(); c++) {
					if (((String[]) House[a])[b].charAt(c) == 'S') {
						i -= b;
						j -= a;
						k -= c;
					}

				}
			}
		}

		ArrayList<int[]> alTorch = new ArrayList<int[]>();
		ArrayList<int[]> alDoor = new ArrayList<int[]>();
		ArrayList<int[]> alLadder = new ArrayList<int[]>();
		// construct building
		for (int a = 0; a < House.length; a++) {
			for (int b = 0; b < ((String[]) House[a]).length; b++) {
				for (int c = 0; c < ((String[]) House[a])[b].length(); c++) {
					if (((String[]) House[a])[b].charAt(c) == 'X' || ((String[]) House[a])[b].charAt(c) == 'S' ) {
						world.setBlockWithNotify(i + b, j + a, k + c,
								Block.planks.blockID);
					}
					if (((String[]) House[a])[b].charAt(c) == 'D') {
						int[] inDoor = { b, a, c };
						alDoor.add(inDoor);
					}
					if (((String[]) House[a])[b].charAt(c) == 'F') {
						int[] inTorch = { b, a, c };
						alTorch.add(inTorch);
					}
					if (((String[]) House[a])[b].charAt(c) == 'W') {
						int[] inLadder = { b, a, c };
						alLadder.add(inLadder);
					}
					if (((String[]) House[a])[b].charAt(c) == 'U') {
						world.setBlockWithNotify(i + b, j + a, k + c,
								Block.glass.blockID);
					}
					if (((String[]) House[a])[b].charAt(c) == 'V') {
						world.setBlockWithNotify(i + b, j + a, k + c,
								Block.grass.blockID);
					}
					if (((String[]) House[a])[b].charAt(c) == ' ') {
						world.setBlockWithNotify(i + b, j + a, k + c,
								0);
					}
					
				}
			}
		}

		// Add Torches
		for (int d = 0; d < alTorch.size(); d++) {
			world.setBlockWithNotify(i + alTorch.get(d)[0], j
					+ alTorch.get(d)[1], k + alTorch.get(d)[2],
					Block.torchWood.blockID);
		}

		// Add Doors
		for (int e = 0; e < alDoor.size(); e++) {
			world.setBlockWithNotify(i + alDoor.get(e)[0],
					j + alDoor.get(e)[1], k + alDoor.get(e)[2],
					Block.doorWood.blockID);
		}
		// Add Ladders
		for (int f = 0; f < alLadder.size(); f++) {
			world.setBlockWithNotify(i + alLadder.get(f)[0],
					j + alLadder.get(f)[1], k + alLadder.get(f)[2],
					Block.ladder.blockID);
		}
	}

	public boolean generate(World world, Random random, int i, int j, int k) {
		boolean canPlace = false;
		for (int l = 0; l < 64; l++) {
			int num = 0;
			for (int test = 0; test < 5; test++) {
				for (int test2 = 0; test2 < 5; test2++) {
						if(world.getBlockId(i+test, j - 1, k+test2) == Block.grass.blockID)
						{
						num += 1;
						}				
				}
			}
			if(num >= 15)
			{
			canPlace = true;
			}
		
			int i1 = (i + random.nextInt(16)) - random.nextInt(16);
			int j1 = (j + random.nextInt(16)) - random.nextInt(16);
			int k1 = (k + random.nextInt(16)) - random.nextInt(16);
			// world.func_20084_d(i1, j1, k1) isair
			if (world.func_20084_d(i1, j1, k1)
					&& canPlace == true
					&& !world.func_20084_d(i1, j1-1, k1)
					&& world.getBlockId(i1, j1-1, k1) != Block.leaves.blockID
					&& world.getBlockId(i1, j1-1, k1) != Block.waterStill.blockID
					&& world.getBlockId(i1, j1-1, k1) != Block.blockIce.blockID
					&& world.func_20084_d(i1-2, j1, k1)
					&& world.func_20084_d(i1, j1, k1-2)
					&& world.func_20084_d(i1, j1, k1+2)
					&& world.func_20084_d(i1+2, j1, k1)
					/*&& world.getBlockId(i1, j1, k1) == Block.grass.blockID*/
					&& Block.grass.canPlaceBlockAt(world, i1, j1, k1)) {
				createHouse(world, i1, j1, k1);
			}
		}

		return true;
	}
}
